define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/users/index' + location.search,
                    add_url: 'member/users/add',
                    edit_url: 'member/users/edit',
                    //del_url: 'member/users/del',
                    multi_url: 'member/users/multi',
                    table: 'users',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'getusers.nickname', title: __('上级用户')},
                        {field: 'getusers.mobile', title: __('上级手机号')},
                        {field: 'uuid', title: __('Uuid')},
                        //{field: 'wx_openid', title: __('Wx_openid')},
                        //{field: 'password', title: __('Password')},
                        //{field: 'pay_password', title: __('Pay_password')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'truename', title: __('Truename')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'headimg', title: __('Headimg'),operate:false, formatter: function (value, row, index) {
                                if(row.headimg == ''){
                                    return '-';
                                }else {
                                    return '<a target="_blank"  href="http://file.mxtang.net/'+ row.headimg+'"><img width="40" height="40" src="http://file.mxtang.net/'+ row.headimg+'"></a>';
                                }
                            }
                        },

                        //{field: 'id_card', title: __('Id_card')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        //{field: 'alipay', title: __('Alipay')},
                        {field: 'level', title: __('Level'), searchList: {"1":__('Level 1'),"2":__('Level 2'),"3":__('Level 3'),"4":__('Level 4')}, formatter: Table.api.formatter.normal},
                        //{field: 'qrcode', title: __('Qrcode')},
                        {field: 'balance', title: __('Balance'), operate:'BETWEEN'},
                        {field: 'lover', title: __('Lover')},
                        //{field: 'province', title: __('Province')},
                        //{field: 'city', title: __('City')},
                        //{field: 'area', title: __('Area')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'first_log', title: __('First_log')},
                        //{field: 'day_log', title: __('Day_log')},
                        //{field: 'sign', title: __('Sign')},
                        {field: 'vip_expire_time', title: __('Vip_expire_time'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'qrcode_url', title: __('Qrcode_url'), formatter: Table.api.formatter.url},
                        {field: 'recharge', title: '', table: table, buttons: [
                                {
                                    name: 'recharge',
                                    text: '充值',
                                    icon: 'fa fa-flash',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'member/users/recharge',
                                }
                            ],events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                        {field: 'super_vip', title: '', table: table, buttons: [
                                {
                                    name: 'super_vip',
                                    text: '设置超级会员',
                                    icon: 'fa fa-flash',
                                    confirm:'确定设为超级会员?',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'member/users/super_vip',
                                    success:function(data, ret){
                                        $(".btn-refresh").trigger("click");//刷新当前页面的数据
                                    }
                                }

                            ],events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                if(row.level==3){
                                    return '';
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    Form.api.bindevent($("form[role=form]"));
    return Controller;
});